#!/bin/sh

# $Id: memo_docker,v 1.17 2022/04/04 14:46:21 gilles Exp gilles $

echo imapsync_docker_timestamp_dockerfile
imapsync_docker_timestamp_dockerfile()
{
	DATE_CURRENT=`date +%Y_%m_%d_%H_%M_%S`
	echo $DATE_CURRENT
	sed -i -e "/just_a_comment_to_force_update/s/comment_to_force_update.*/comment_to_force_update $DATE_CURRENT/" Dockerfile
	ci -l -f -m"Changing timestamp to $DATE_CURRENT with imapsync_docker_timestamp_dockerfile" Dockerfile
}

echo imapsync_docker_build
imapsync_docker_build()
{
    docker build -t gilleslamiral/imapsync . \
        && imapsync_docker_add_tag_version && echo Build SUCCEEDED || echo Build FAILED
}

echo imapsync_docker_version
imapsync_docker_version()
{
        docker run gilleslamiral/imapsync imapsync --version
}

echo imapsync_local_docker_version
imapsync_local_docker_version()
{
        docker run gilleslamiral/imapsync /imapsync --version
}

echo imapsync_docker_add_tag_version
imapsync_docker_add_tag_version()
{
    docker images gilleslamiral/imapsync
    echo 
    version=`imapsync_docker_version`
    echo $version
    docker tag gilleslamiral/imapsync:latest gilleslamiral/imapsync:$version
    docker images gilleslamiral/imapsync
}



echo imapsync_docker_testslive
imapsync_docker_testslive()
{
    echo docker run gilleslamiral/imapsync imapsync --testslive "$@"
    docker run gilleslamiral/imapsync imapsync --testslive "$@"
}

echo imapsync_docker_testslive6
imapsync_docker_testslive6()
{
    echo docker run gilleslamiral/imapsync imapsync --testslive6 "$@"
    docker run gilleslamiral/imapsync imapsync --testslive6 "$@"
}

echo imapsync_docker_tests
imapsync_docker_tests()
{
    echo docker run gilleslamiral/imapsync imapsync --tests
    docker run gilleslamiral/imapsync imapsync --tests
}

echo imapsync_docker_envfile
imapsync_docker_envfile()
{
        docker run --env-file ./secret.txt gilleslamiral/imapsync imapsync \
        --host1 test1.lamiral.info --user1 test1 \
        --host2 test2.lamiral.info --user2 test2  --justlogin
}

echo docker_delete_all_images
docker_delete_all_images()
{
    docker rm `docker ps -a -q`
    docker rmi `docker images -q`
}

echo docker_system_prune
docker_system_prune()
{
        docker system prune
}

echo docker_delete_dandling_images
docker_delete_dandling_images()
{
	docker images
	dandling_images=`docker images -f dangling=true -q`
	exited_containers=`docker ps -a -f status=exited -q`
	test -n "$exited_containers" && docker rm $exited_containers
	test -n "$dandling_images"   && docker rmi $dandling_images
	docker images
}


echo imapsync_docker_rebuild_from_scratch
imapsync_docker_rebuild_from_scratch()
{
    delete_all_images
    docker images
    imapsync_docker_build
}


echo is_release_number
is_release_number()
{
        echo "$1" | perl -pe 'm/^\d\.\d+$/ || exit 1' 
}

echo imapsync_docker_upload
imapsync_docker_upload()
{
        version=`imapsync_docker_version`
        version_local=`imapsync_local_docker_version`
        
        # upload only after successful tests
        set -x \
        && is_release_number "$version" \
        && is_release_number "$version_local" \
        && test "$version" = "$version_local" \
        && rcsdiff imapsync Dockerfile memo_docker \
        && imapsync_docker_testslive \
        && imapsync_docker_tests \
        && docker run gilleslamiral/imapsync true \
        && docker run gilleslamiral/imapsync diff /imapsync /usr/bin/imapsync \
        && docker login --username=gilleslamiral --password=`cat $HOME/var/pass/secret.docker` \
        && docker push gilleslamiral/imapsync:latest \
        && docker push gilleslamiral/imapsync:$version \
        && set +x && echo imapsync push SUCCEEDED || echo imapsync push FAILED
        set +x
}


